home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / games / mpuz.el.z / mpuz.el
Encoding:
Text File  |  1998-05-21  |  14.5 KB  |  446 lines

  1. ;;; mpuz.el --- multiplication puzzle for XEmacs
  2.  
  3. ;; Copyright (C) 1990 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Philippe Schnoebelen <phs@lifia.imag.fr>
  6. ;; Keywords: games
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. ;; 02111-1307, USA.
  24.  
  25. ;;; Synched up with: FSF 19.34.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; When this package is loaded, `M-x mpuz' generates a random multiplication
  30. ;; puzzle.  This is a multiplication example in which each digit has been
  31. ;; consistently replaced with some letter.  Your job is to reconstruct
  32. ;; the original digits.  Type `?' while the mode is active for detailed help.
  33.  
  34. ;;; Code:
  35.  
  36. (random t)                ; randomize
  37.  
  38. (defvar mpuz-silent nil
  39.   "*Set this to T if you don't want dings on inputs.")
  40.  
  41. (defun mpuz-ding ()
  42.   "Dings, unless global variable `mpuz-silent' forbids it."
  43.   (or mpuz-silent (ding t)))
  44.  
  45.  
  46. ;; Mpuz mode and keymaps
  47. ;;----------------------
  48. (defvar mpuz-mode-hook nil)
  49.  
  50. (defvar mpuz-mode-map nil
  51.   "Local keymap to use in Mult Puzzle.")
  52.  
  53. (if mpuz-mode-map nil
  54.     (setq mpuz-mode-map (make-sparse-keymap))
  55.     (define-key mpuz-mode-map "a" 'mpuz-try-letter)
  56.     (define-key mpuz-mode-map "b" 'mpuz-try-letter)
  57.     (define-key mpuz-mode-map "c" 'mpuz-try-letter)
  58.     (define-key mpuz-mode-map "d" 'mpuz-try-letter)
  59.     (define-key mpuz-mode-map "e" 'mpuz-try-letter)
  60.     (define-key mpuz-mode-map "f" 'mpuz-try-letter)
  61.     (define-key mpuz-mode-map "g" 'mpuz-try-letter)
  62.     (define-key mpuz-mode-map "h" 'mpuz-try-letter)
  63.     (define-key mpuz-mode-map "i" 'mpuz-try-letter)
  64.     (define-key mpuz-mode-map "j" 'mpuz-try-letter)
  65.     (define-key mpuz-mode-map "A" 'mpuz-try-letter)
  66.     (define-key mpuz-mode-map "B" 'mpuz-try-letter)
  67.     (define-key mpuz-mode-map "C" 'mpuz-try-letter)
  68.     (define-key mpuz-mode-map "D" 'mpuz-try-letter)
  69.     (define-key mpuz-mode-map "E" 'mpuz-try-letter)
  70.     (define-key mpuz-mode-map "F" 'mpuz-try-letter)
  71.     (define-key mpuz-mode-map "G" 'mpuz-try-letter)
  72.     (define-key mpuz-mode-map "H" 'mpuz-try-letter)
  73.     (define-key mpuz-mode-map "I" 'mpuz-try-letter)
  74.     (define-key mpuz-mode-map "J" 'mpuz-try-letter)
  75.     (define-key mpuz-mode-map "\C-g" 'mpuz-offer-abort)
  76.     (define-key mpuz-mode-map "?" 'describe-mode))
  77.  
  78. (defun mpuz-mode ()
  79.   "Multiplication puzzle mode.
  80.  
  81. You have to guess which letters stand for which digits in the
  82. multiplication displayed inside the `*Mult Puzzle*' buffer.
  83.  
  84. You may enter a guess for a letter's value by typing first the letter,
  85. then the digit.  Thus, to guess that A=3, type A 3.
  86.  
  87. To leave the game to do other editing work, just switch buffers.
  88. Then you may resume the game with M-x mpuz.
  89. You may abort a game by typing \\<mpuz-mode-map>\\[mpuz-offer-abort]."
  90.   (interactive)
  91.   (setq major-mode 'mpuz-mode
  92.     mode-name  "Mult Puzzle")
  93.   (use-local-map mpuz-mode-map)
  94.   (run-hooks 'mpuz-mode-hook))
  95.  
  96.  
  97. ;; Some variables for statistics
  98. ;;------------------------------
  99. (defvar mpuz-nb-errors 0
  100.   "Number of errors made in current game.")
  101.  
  102. (defvar mpuz-nb-completed-games 0
  103.   "Number of games completed.")
  104.  
  105. (defvar mpuz-nb-cumulated-errors 0
  106.   "Number of errors made in previous games.")
  107.  
  108.  
  109. ;; Some variables for game tracking
  110. ;;---------------------------------
  111. (defvar mpuz-in-progress nil
  112.   "True if a game is currently in progress.")
  113.  
  114. (defvar mpuz-found-digits (make-vector 10 nil)
  115.   "A vector recording which digits have been decrypted.")
  116.  
  117. (defmacro mpuz-digit-solved-p (digit)
  118.   (list 'aref 'mpuz-found-digits digit))
  119.  
  120.  
  121. ;; A puzzle uses a permutation of [0..9] into itself.
  122. ;; We use both the permutation and its inverse.
  123. ;;---------------------------------------------------
  124. (defvar mpuz-digit-to-letter (make-vector 10 0)
  125.   "A permutation from [0..9] to [0..9].")
  126.  
  127. (defvar mpuz-letter-to-digit (make-vector 10 0)
  128.   "The inverse of mpuz-digit-to-letter.")
  129.  
  130. (defmacro mpuz-to-digit (letter)
  131.   (list 'aref 'mpuz-letter-to-digit letter))
  132.  
  133. (defmacro mpuz-to-letter (digit)
  134.   (list 'aref 'mpuz-digit-to-letter digit))
  135.  
  136. (defun mpuz-build-random-perm ()
  137.   "Initialize puzzle coding with a random permutation."
  138.   (let ((letters (list 0 1 2 3 4 5 6 7 8 9)) ; new cons cells, because of delq
  139.     (index 10)
  140.     elem)
  141.     (while letters
  142.       (setq elem    (nth (random index) letters)
  143.         letters (delq elem letters)
  144.         index   (1- index))
  145.       (aset mpuz-digit-to-letter index elem)
  146.       (aset mpuz-letter-to-digit elem index))))
  147.  
  148.  
  149. ;; A puzzle also uses a board displaying a multiplication.
  150. ;; Every digit appears in the board, crypted or not.
  151. ;;------------------------------------------------------
  152. (defvar mpuz-board (make-vector 10 nil)
  153.   "The board associates to any digit the list of squares where it appears.")
  154.  
  155. (defun mpuz-put-digit-on-board (number square)
  156.   "Put (last digit of) NUMBER on SQUARE of the puzzle board."
  157.   ;; i.e. push SQUARE on NUMBER square-list
  158.   (setq number (% number 10))
  159.   (aset mpuz-board number (cons square (aref mpuz-board number))))
  160.  
  161. (defun mpuz-check-all-solved ()
  162.   "Check whether all digits have been solved. Return t if yes."
  163.   (catch 'found
  164.     (let ((digit -1))
  165.       (while (> 10 (setq digit (1+ digit)))
  166.     (if (and (not (mpuz-digit-solved-p digit)) ; unsolved
  167.          (aref mpuz-board digit)) ; and appearing in the puzzle !
  168.         (throw 'found nil))))
  169.     t))
  170.  
  171.  
  172. ;; To build a puzzle, we take two random numbers and multiply them.
  173. ;; We also take a random permutation for encryption.
  174. ;; The random numbers are only use to see which digit appears in which square
  175. ;; of the board. Everything is stored in individual squares.
  176. ;;---------------------------------------------------------------------------
  177. (defun mpuz-random-puzzle ()
  178.   "Draw random values to be multiplied in a puzzle."
  179.   (mpuz-build-random-perm)
  180.   (fillarray mpuz-board nil)        ; erase the board
  181.   (let (A B C D E)
  182.     ;; A,B,C,D & E, are the five rows of our multiplication.
  183.     ;; Choose random values, discarding uninteresting cases.
  184.     (while (progn
  185.          (setq A (random 1000)
  186.            B (random 100)
  187.            C (* A (% B 10))
  188.            D (* A (/ B 10))
  189.            E (* A B))
  190.          (or (< C 1000) (< D 1000)))) ; forbid leading zeros in C or D
  191.     ;; Individual digits are now put on their respective squares.
  192.     ;; [NB: A square is a pair <row,column> of the screen.]
  193.     (mpuz-put-digit-on-board A         '(2 . 9))
  194.     (mpuz-put-digit-on-board (/ A 10)     '(2 . 7))
  195.     (mpuz-put-digit-on-board (/ A 100)     '(2 . 5))
  196.     (mpuz-put-digit-on-board B         '(4 . 9))
  197.     (mpuz-put-digit-on-board (/ B 10)     '(4 . 7))
  198.     (mpuz-put-digit-on-board C         '(6 . 9))
  199.     (mpuz-put-digit-on-board (/ C 10)     '(6 . 7))
  200.     (mpuz-put-digit-on-board (/ C 100)     '(6 . 5))
  201.     (mpuz-put-digit-on-board (/ C 1000)     '(6 . 3))
  202.     (mpuz-put-digit-on-board D         '(8 . 7))
  203.     (mpuz-put-digit-on-board (/ D 10)     '(8 . 5))
  204.     (mpuz-put-digit-on-board (/ D 100)     '(8 . 3))
  205.     (mpuz-put-digit-on-board (/ D 1000)     '(8 . 1))
  206.     (mpuz-put-digit-on-board E         '(10 . 9))
  207.     (mpuz-put-digit-on-board (/ E 10)     '(10 . 7))
  208.     (mpuz-put-digit-on-board (/ E 100)     '(10 . 5))
  209.     (mpuz-put-digit-on-board (/ E 1000)     '(10 . 3))
  210.     (mpuz-put-digit-on-board (/ E 10000) '(10 . 1))))
  211.  
  212. ;; Display
  213. ;;--------
  214. (defconst mpuz-framework
  215.   "
  216.      . . .
  217.                    Number of errors (this game): 0
  218.     x  . .
  219.    -------
  220.    . . . .
  221.                         Number of completed games: 0
  222.  . . . .
  223.  ---------              Average number of errors: 0.00
  224.  . . . . ."
  225.   "The general picture of the puzzle screen, as a string.")
  226.  
  227. (defun mpuz-create-buffer ()
  228.   "Create (or recreate) the puzzle buffer. Return it."
  229.   (let ((buff (get-buffer-create "*Mult Puzzle*")))
  230.     (save-excursion
  231.       (set-buffer buff)
  232.       (let ((buffer-read-only nil))
  233.     (erase-buffer)
  234.     (insert mpuz-framework)
  235.     (mpuz-paint-board)
  236.     (mpuz-paint-errors)
  237.     (mpuz-paint-statistics)))
  238.     buff))
  239.  
  240. (defun mpuz-paint-errors ()
  241.   "Paint error count on the puzzle screen."
  242.   (mpuz-switch-to-window)
  243.   (let ((buffer-read-only nil))
  244.     (goto-line 3)
  245.     (move-to-column 49)
  246.     (mpuz-delete-line)
  247.     (insert (prin1-to-string mpuz-nb-errors))))
  248.  
  249. (defun mpuz-paint-statistics ()
  250.   "Paint statistics about previous games on the puzzle screen."
  251.   (let* ((mean (if (zerop mpuz-nb-completed-games) 0
  252.            (/ (+ mpuz-nb-completed-games (* 200 mpuz-nb-cumulated-errors))
  253.               (* 2 mpuz-nb-completed-games))))
  254.      (frac-part (% mean 100)))
  255.     (let ((buffer-read-only nil))
  256.       (goto-line 7)
  257.       (move-to-column 51)
  258.       (mpuz-delete-line)
  259.       (insert (prin1-to-string mpuz-nb-completed-games))
  260.       (goto-line 9)
  261.       (move-to-column 50)
  262.       (mpuz-delete-line)
  263.       (insert (format "%d.%d%d" (/ mean 100) (/ frac-part 10) (% frac-part 10))))))
  264.  
  265. (defun mpuz-paint-board ()
  266.   "Paint board situation on the puzzle screen."
  267.   (mpuz-switch-to-window)
  268.   (let ((letter -1))
  269.     (while (> 10 (setq letter (1+ letter)))
  270.       (mpuz-paint-digit (mpuz-to-digit letter))))
  271.   (goto-char (point-min)))
  272.  
  273. (defun mpuz-paint-digit (digit)
  274.   "Paint all occurrences of DIGIT on the puzzle board."
  275.   ;; (mpuz-switch-to-window)
  276.   (let ((char (if (mpuz-digit-solved-p digit)
  277.           (+ digit ?0)
  278.           (+ (mpuz-to-letter digit) ?A)))
  279.     (square-l (aref mpuz-board digit)))
  280.     (let ((buffer-read-only nil))
  281.       (while square-l
  282.     (goto-line (car (car square-l)))    ; line before column !
  283.     (move-to-column (cdr (car square-l)))
  284.     (insert char)
  285.     (delete-char 1)
  286.     (backward-char 1)
  287.     (setq square-l (cdr square-l))))))
  288.  
  289. (defun mpuz-delete-line ()
  290.   "Clear from point to next newline."    ; & put nothing in the kill ring
  291.   (while (not (= ?\n (char-after (point))))
  292.     (delete-char 1)))
  293.  
  294. (defun mpuz-get-buffer ()
  295.   "Get the puzzle buffer if it exists."
  296.   (get-buffer "*Mult Puzzle*"))
  297.  
  298. (defun mpuz-switch-to-window ()
  299.   "Find or create the Mult-Puzzle buffer, and display it."
  300.   (let ((buff (mpuz-get-buffer)))
  301.     (or buff (setq buff (mpuz-create-buffer)))
  302.     (switch-to-buffer buff)
  303.     (or buffer-read-only (toggle-read-only))
  304.     (mpuz-mode)))
  305.  
  306.  
  307. ;; Game control
  308. ;;-------------
  309. (defun mpuz-abort-game ()
  310.   "Abort any puzzle in progress."
  311.   (message "Mult Puzzle aborted.")
  312.   (setq mpuz-in-progress nil
  313.     mpuz-nb-errors 0)
  314.   (fillarray mpuz-board nil)
  315.   (let ((buff (mpuz-get-buffer)))
  316.     (if buff (kill-buffer buff))))
  317.  
  318. (defun mpuz-start-new-game ()
  319.   "Start a new puzzle."
  320.   (message "Here we go...")
  321.   (setq mpuz-nb-errors 0
  322.     mpuz-in-progress t)
  323.   (fillarray mpuz-found-digits nil)    ; initialize mpuz-found-digits
  324.   (mpuz-random-puzzle)
  325.   (mpuz-switch-to-window)
  326.   (mpuz-paint-board)
  327.   (mpuz-paint-errors)
  328.   (mpuz-ask-for-try))
  329.  
  330. (defun mpuz-offer-new-game ()
  331.   "Ask if user wants to start a new puzzle."
  332.   (if (y-or-n-p "Start a new game ")
  333.       (mpuz-start-new-game)
  334.       (message "OK. I won't.")))
  335.  
  336. ;;;###autoload
  337. (defun mpuz ()
  338.   "Multiplication puzzle with GNU Emacs."
  339.   ;; Main entry point
  340.   (interactive)
  341.   (mpuz-switch-to-window)
  342.   (if mpuz-in-progress
  343.       (mpuz-offer-abort)
  344.       (mpuz-start-new-game)))
  345.  
  346. (defun mpuz-offer-abort ()
  347.   "Ask if user wants to abort current puzzle."
  348.   (interactive)
  349.   (if (y-or-n-p "Abort game ")
  350.       (mpuz-abort-game)
  351.       (mpuz-ask-for-try)))
  352.  
  353. (defun mpuz-ask-for-try ()
  354.   "Ask for user proposal in puzzle."
  355.   (message "Your try ?"))
  356.  
  357. (defun mpuz-try-letter ()
  358.   "Propose a digit for a letter in puzzle."
  359.   (interactive)
  360.   (if mpuz-in-progress
  361.       (let (letter-char digit digit-char message)
  362.     (setq letter-char (upcase last-command-char)
  363.           digit (mpuz-to-digit (- letter-char ?A)))
  364.     (cond ((mpuz-digit-solved-p digit)
  365.            (message "%c already solved." letter-char))
  366.           ((null (aref mpuz-board digit))
  367.            (message "%c does not appear." letter-char))
  368.           ((progn (message "%c = " letter-char)
  369.               ;; <char> has been entered.
  370.               ;; Print "<char> =" and
  371.               ;; read <num> or = <num>
  372.               (setq digit-char (read-char))
  373.               (if (eq digit-char ?=)
  374.               (setq digit-char (read-char)))
  375.               (message "%c = %c" letter-char digit-char)
  376.               (or (> digit-char ?9) (< digit-char ?0))) ; bad input
  377.            (ding t))
  378.           (t
  379.            (mpuz-try-proposal letter-char digit-char))))
  380.       (mpuz-offer-new-game)))
  381.  
  382. (defun mpuz-try-proposal (letter-char digit-char)
  383.   "Propose LETTER-CHAR as code for DIGIT-CHAR."
  384.   (let* ((letter (- letter-char ?A))
  385.      (digit (- digit-char ?0))
  386.      (correct-digit (mpuz-to-digit letter)))
  387.     (cond ((mpuz-digit-solved-p correct-digit)
  388.        (message "%c has already been found."))
  389.       ((= digit correct-digit)
  390.        (message "%c = %c correct !" letter-char digit-char)
  391.        (mpuz-ding)
  392.        (mpuz-correct-guess digit))
  393.       (t ;;; incorrect guess
  394.        (message "%c = %c incorrect !" letter-char digit-char)
  395.        (mpuz-ding)
  396.        (setq mpuz-nb-errors (1+ mpuz-nb-errors))
  397.        (mpuz-paint-errors)))))
  398.  
  399. (defun mpuz-correct-guess (digit)
  400.   "Handle correct guessing of DIGIT."
  401.   (aset mpuz-found-digits digit t)    ; Mark digit as solved
  402.   (mpuz-paint-digit digit)        ; Repaint it (now as a digit)
  403.   (if (mpuz-check-all-solved)
  404.       (mpuz-close-game)))
  405.  
  406. (defun mpuz-close-game ()
  407.   "Housecleaning when puzzle has been solved."
  408.   (setq mpuz-in-progress nil
  409.     mpuz-nb-cumulated-errors (+ mpuz-nb-cumulated-errors mpuz-nb-errors)
  410.     mpuz-nb-completed-games (1+ mpuz-nb-completed-games))
  411.   (mpuz-paint-statistics)
  412.   (let ((message (mpuz-congratulate)))
  413.     (message message)
  414.     (sit-for 4)
  415.     (if (y-or-n-p (concat message "  Start a new game "))
  416.     (mpuz-start-new-game)
  417.     (message "Good Bye !"))))
  418.  
  419. (defun mpuz-congratulate ()
  420.   "Build a congratulation message when puzzle is solved."
  421.   (format "Puzzle solved with %d errors. %s"
  422.        mpuz-nb-errors
  423.        (cond ((= mpuz-nb-errors 0)          "That's perfect !")
  424.          ((= mpuz-nb-errors 1)          "That's very good !")
  425.          ((= mpuz-nb-errors 2)          "That's good.")
  426.          ((= mpuz-nb-errors 3)          "That's not bad.")
  427.          ((= mpuz-nb-errors 4)          "That's not too bad...")
  428.          ((and (>= mpuz-nb-errors 5)
  429.                (< mpuz-nb-errors 10)) "That's bad !")
  430.          ((and (>= mpuz-nb-errors 10)
  431.                (< mpuz-nb-errors 15)) "That's awful.")
  432.          ((>= mpuz-nb-errors 15)      "That's not serious."))))
  433.  
  434. (defun mpuz-show-solution ()
  435.   "Display solution for debugging purposes."
  436.   (interactive)
  437.   (mpuz-switch-to-window)
  438.   (let (digit list)
  439.     (setq digit -1)
  440.     (while (> 10 (setq digit (1+ digit)))
  441.       (or (mpuz-digit-solved-p digit)
  442.       (setq list (cons digit list))))
  443.     (mapcar 'mpuz-correct-guess list)))
  444.  
  445. ;;; mpuz.el ends here
  446.